/*
 * PictureFrame.java
 *
 * Created on March 16, 2008, 9:28 AM
 */

package BuddyTemplate;
import java.util.*;
import java.io.*; 
import javax.swing.*;


/**
 *
 * @author  Administrator
 */
public class PictureFrame extends javax.swing.JFrame {
    
    QuestionInterface QuestionCollection;
    /*Creating the constructors which pass the nextQuestion information, score, 
     *and current questtion information
     */
    public PictureFrame(int theScore, QuestionInterface TheQuestionCollection, Question temp) {
        SaveGame.score = theScore;
        initComponents();
        
        QuestionCollection = TheQuestionCollection;
        
        Question<String, ImageIcon> temp1;
        //try{
            temp1 = temp;
            
            //Gets the location of the pictures
            QuestionArea.setText((String)temp1.getQuestion());
            Picture1.setIcon(temp1.choices.get(0));
            Picture2.setIcon(temp1.choices.get(1));
            Picture3.setIcon(temp1.choices.get(2));
            Picture4.setIcon(temp1.choices.get(3));
        //}
        //catch (Exception e){
            
        //    System.out.println(e.getLocalizedMessage() + "Casting Error");
    
            
       // }

    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        QuestionArea = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        Picture3 = new javax.swing.JLabel();
        Picture2 = new javax.swing.JLabel();
        Picture4 = new javax.swing.JLabel();
        Picture1 = new javax.swing.JLabel();
        AnsA = new javax.swing.JCheckBox();
        AnsC = new javax.swing.JCheckBox();
        AnsB = new javax.swing.JCheckBox();
        AnsD = new javax.swing.JCheckBox();
        Return = new javax.swing.JButton();
        NextQuestion = new javax.swing.JButton();
        AnsCheck = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Study Buddy");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Question", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18)));

        jScrollPane1.setFont(new java.awt.Font("Tahoma", 0, 14));

        QuestionArea.setColumns(20);
        QuestionArea.setRows(5);
        jScrollPane1.setViewportView(QuestionArea);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 858, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Answer", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18)));

        AnsA.setText("A");

        AnsC.setText("C");

        AnsB.setText("B");

        AnsD.setText("D");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(AnsA)
                    .addComponent(AnsC))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Picture3, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Picture1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(49, 49, 49)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(AnsB)
                        .addGap(18, 18, 18)
                        .addComponent(Picture2, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(AnsD)
                        .addGap(18, 18, 18)
                        .addComponent(Picture4, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(94, 94, 94))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Picture1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Picture2, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(AnsA))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(AnsB)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Picture3, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Picture4, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addComponent(AnsD))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addComponent(AnsC)))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        Return.setFont(new java.awt.Font("Tahoma", 1, 14));
        Return.setText("Main Menu");
        Return.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ReturnActionPerformed(evt);
            }
        });

        NextQuestion.setFont(new java.awt.Font("Tahoma", 1, 14));
        NextQuestion.setText("Next Question");
        NextQuestion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NextQuestionActionPerformed(evt);
            }
        });

        AnsCheck.setFont(new java.awt.Font("Tahoma", 0, 18));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(55, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(Return, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(97, 97, 97)
                        .addComponent(AnsCheck, javax.swing.GroupLayout.PREFERRED_SIZE, 423, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(NextQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 894, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(51, 51, 51))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NextQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Return, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AnsCheck, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ReturnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReturnActionPerformed
        this.setVisible(false);
        dispose();
        
        /*BuddyLoader BL = new BuddyLoader();
    BL.setVisible(true);
    this.setVisible(false);
    dispose();
    */
}//GEN-LAST:event_ReturnActionPerformed

    private void NextQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NextQuestionActionPerformed
        ArrayList<Boolean> ansprovided = new ArrayList<Boolean>();
        int Answer = 0;
        
        //This will get the selection from the user
        ansprovided.add(0, AnsA.isSelected());
        ansprovided.add(1, AnsB.isSelected());
        ansprovided.add(2, AnsC.isSelected());
        ansprovided.add(3, AnsD.isSelected());
        
        //Checks the score of the user.
        System.out.println(QuestionCollection.CheckAnswer(ansprovided));
        Answer = QuestionCollection.CheckAnswer(ansprovided);
        
        //Checking if this is the users first attempt to solve the question
        if (checker == true)
        {
            SaveGame.score = SaveGame.score + Answer;
            checker = false;
        }
        
        /*If the users get the answer correct, it will check if the current
         * question is the last question if not, it will select the proper frame
         * to use for the next question.
         */        
        if (Answer == 4)
        {

            if (QuestionCollection.isFinal())
            {
                JOptionPane.showMessageDialog(null, "No more questions are avaiable.");
                FinalScoreFrame FSF = new FinalScoreFrame(SaveGame.score);
                FSF.setVisible(true);
                this.setVisible(false);
                this.dispose();
            }
            
            else
            {    
                Question thenextquestion = QuestionCollection.nextQuestion();
                
                if(thenextquestion.choices.get(0) instanceof String && thenextquestion.getQuestion() instanceof String){
                    QuestionFrame nextQuestion = new QuestionFrame(SaveGame.score, QuestionCollection, thenextquestion);
                    nextQuestion.setVisible(true);
                }
                
                else if(thenextquestion.choices.get(0) instanceof ImageIcon && thenextquestion.getQuestion() instanceof String){
                    PictureFrame nextQuestion = new PictureFrame(SaveGame.score, QuestionCollection, thenextquestion);
                    nextQuestion.setVisible(true);
                }
                
                else if(thenextquestion.choices.get(0) instanceof String && thenextquestion.getQuestion() instanceof ImageIcon){
                    PictureChoiceFrame nextQuestion = new PictureChoiceFrame(SaveGame.score, QuestionCollection, thenextquestion);
                    nextQuestion.setVisible(true);
                }
                
                else{
                    QuestionFrame nextQuestion = new QuestionFrame(SaveGame.score, QuestionCollection, thenextquestion);
                    nextQuestion.setVisible(true);
                }
                

                this.setVisible(false);
                this.dispose();
            }
        }
        
        //If the answer is incorrent, the user will get to try again.
        else
        {
            AnsCheck.setText("Incorrect, please try again.");
        }
    }//GEN-LAST:event_NextQuestionActionPerformed
    
    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox AnsA;
    private javax.swing.JCheckBox AnsB;
    private javax.swing.JCheckBox AnsC;
    private javax.swing.JLabel AnsCheck;
    private javax.swing.JCheckBox AnsD;
    private javax.swing.JButton NextQuestion;
    private javax.swing.JLabel Picture1;
    private javax.swing.JLabel Picture2;
    private javax.swing.JLabel Picture3;
    private javax.swing.JLabel Picture4;
    private javax.swing.JTextArea QuestionArea;
    private javax.swing.JButton Return;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    public boolean checker = true;
    
}
